package com.goldgov.code.meta;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.goldgov.code.utils.StringUtils;

public class ModuleInfo {
	
	private String moduleID;
	private String packageName;
	private String name;
	private String explain;
	private String tableName;
	private FieldInfo[] fields;
	private String parentID;
	private RelationInfo relation;
	
	private String projectID;
	
	/*
	 * 如果设置前缀，则在根据表生成代码生成时会忽略前缀生成代码，假设表名为：TEST_TABLE，前缀为：TEST，生成代码的对象为Table，而不是TestTable
	 */
	private String tablePrefix;
	
	private int fieldNum;
	
	
	public String getName() {
		if(name == null) {
			name = StringUtils.upperFirstChar(StringUtils.clearSeparator(tableName, '_'));
		}
		if(tablePrefix != null && name.toLowerCase().startsWith(tablePrefix.toLowerCase())) {
			name = name.substring(tablePrefix.length());
		}
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getExplain() {
		if(explain == null) {
			return name;
		}
		return explain;
	}
	public void setExplain(String explain) {
		this.explain = explain;
	}
	public RelationInfo getRelation() {
		return relation;
	}
	public void setRelation(RelationInfo relation) {
		this.relation = relation;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public FieldInfo[] getFields() {
		return fields;
	}
	public void setFields(FieldInfo[] fields) {
		this.fields = fields;
	}
	
	public String getTablePrefix() {
		return tablePrefix;
	}
	public void setTablePrefix(String tablePrefix) {
		this.tablePrefix = tablePrefix;
	}
	public String getPackageName() {
		if(packageName == null) {
			packageName = getName().toLowerCase();
		}
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public String getModuleID() {
		return moduleID;
	}
	public void setModuleID(String moduleID) {
		this.moduleID = moduleID;
	}
	
	public String getProjectID() {
		return projectID;
	}
	public void setProjectID(String projectID) {
		this.projectID = projectID;
	}
	
	public String getParentID() {
		return parentID;
	}
	public void setParentID(String parentID) {
		this.parentID = parentID;
	}
	
	public int getFieldNum() {
		return fieldNum;
	}
	public void setFieldNum(int fieldNum) {
		this.fieldNum = fieldNum;
	}
	
	public List<FieldInfo> getQueryFields() {
		List<FieldInfo> queryList = new ArrayList<>();
		for (FieldInfo fieldInfo : fields) {
			Constraint constraint = fieldInfo.getConstraint();
			if(constraint != null && constraint.getQueryType() != null) {
				queryList.add(fieldInfo);
			}
		}
		return queryList;
	}
	
	@JsonIgnore
	public List<FieldInfo> getRequiredFields() {
		List<FieldInfo> queryList = new ArrayList<>();
		for (FieldInfo fieldInfo : fields) {
			Constraint constraint = fieldInfo.getConstraint();
			if(constraint != null && constraint.isRequired()) {
				queryList.add(fieldInfo);
			}
		}
		return queryList;
	}
	
	@JsonIgnore
	public FieldInfo getPkField() {
		if(fields != null && fields.length > 0) {
			for (FieldInfo fieldInfo : fields) {
				if(fieldInfo.getConstraint() != null && fieldInfo.getConstraint().isPrimaryKey()) {
					return fieldInfo;
				}
			}
			throw new RuntimeException(tableName + "表未设置主键");
		}
		return null;
	}
	
	public boolean getHasQueryFiled() {
		if(fields != null) {
			for (FieldInfo fieldInfo : fields) {
				Constraint constraint = fieldInfo.getConstraint();
				if(constraint != null && constraint.getQueryType() != null) {
					return true;
				}
			}
		}
		return false;
	}
}
